package cn.smile.commons.response;

import cn.smile.commons.constant.NumberConstant;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * 微信小程序返回对象
 *
 * @author admin
 */
@Data
public class MyResult<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 8676025151552357756L;
    /**
     * 错误编码
     */
    private Integer retCode = NumberConstant.ZERO;

    /**
     * 返回处理消息
     */
    private String message = "";

    /**
     * 返回数据对象 data
     */
    private T data;

    public MyResult() {
    }

    public static <T> MyResult<T> OK() {
        MyResult<T> r = new MyResult<>();
        r.setRetCode(MyResponseCode.SUCCESS.code());
        r.setMessage(MyResponseCode.SUCCESS.message());
        return r;
    }

    public static <T> MyResult<T> OK(T data) {
        MyResult<T> r = new MyResult<>();
        r.setRetCode(MyResponseCode.SUCCESS.code());
        r.setMessage(MyResponseCode.SUCCESS.message());
        r.setData(data);
        return r;
    }

    public static <T> MyResult<T> OK(String msg, T data) {
        MyResult<T> r = new MyResult<>();
        r.setRetCode(MyResponseCode.SUCCESS.code());
        r.setMessage(msg);
        r.setData(data);
        return r;
    }

    public static <T> MyResult<T> error(MyResponseCode entity, T data) {
        MyResult<T> r = new MyResult<>();
        r.setRetCode(entity.code());
        r.setMessage(entity.message());
        r.setData(data);
        return r;
    }

    @JsonIgnore
    private String onlTable;
}
